package co.wizardattack.activities;

import java.util.Collection;

import com.example.wizardattack.R;

import protocol.ConnectionDiscover;
import protocol.OnConnectionUpdatesListener;
import protocol.OnNewMessageListener;
import protocol.PairedConnection;
import protocol.Print;
import protocol.User;
import co.wizardattack.connection.MessageDistpacher;
import co.wizardattack.connection.NetworkUtility;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class FindOpponentActivity extends Activity implements OnClickListener, OnItemClickListener, OnConnectionUpdatesListener {

	// Visual elements
	private ArrayAdapter<String> listAdapter;
	private ListView listView;
	private EditText yourName;
	private Button changeName;
	private Button connectButton;

	// Communication elements
	private ConnectionDiscover connectionDiscover;
	private PairedConnection temporaryPair;
	private boolean whileGettingConnected;
	private int indexToConnect;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_find_opponent);

		//		Button b = (Button) findViewById(R.id.changeName);
		//		b.setOnClickListener(this);

		init();

		connectionDiscover = new ConnectionDiscover(yourName.getText().toString(),NetworkUtility.getIpAddr((WifiManager) getSystemService(WIFI_SERVICE)), this, MessageDistpacher.INSTANCE);
	}

	private void init() {
		changeName = (Button) findViewById(R.id.changeNameButton);
		changeName.setOnClickListener(this);
		connectButton = (Button) findViewById(R.id.connectButton);
		connectButton.setOnClickListener(this);
		yourName = (EditText) findViewById(R.id.yourName);
		yourName.setText("android");
		listView = (ListView) findViewById(R.id.usersList);
		listView.setOnItemClickListener(this);
		listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, 0);
		listAdapter.add("Jugador 1");
		listAdapter.add("Jugador 2");
		listView.setAdapter(listAdapter);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
//		getMenuInflater().inflate(R.menu., menu);
		return true;
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.changeNameButton:
			if(yourName.getText().length() < 3)
				Toast.makeText(this, "The name is too short", 0).show();
			else {
				try {connectionDiscover.changeUsername(yourName.getText().toString());} catch (Exception e) {}
			}
			break;

		case R.id.connectButton:
			whileGettingConnected = false;
			if(connectionDiscover.connectTo(indexToConnect)) {
				Print.print("wizardgame","Starting BattleActivity");
				Intent intent = new Intent(FindOpponentActivity.this, BattleActivity.class);
				intent.putExtra(BattleActivity.BATTLE_MODE, BattleActivity.ATTACKING_MODE);
				startActivity(intent);
			} else {
				Toast.makeText(getApplicationContext(), "Unreachable user", 0).show();
			}
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
		this.indexToConnect = pos;
		Toast.makeText(getApplicationContext(), "Will connect to "+listAdapter.getItem(pos), 0).show();
	}
	
	@Override
	public void onUsersListUpdated(final Collection<User> users) {
		if(whileGettingConnected) {
			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					listAdapter.clear();
					for (User user : users)
						listAdapter.add(user.toString());
					listAdapter.notifyDataSetChanged();
				}
			});
		}
	}

	@Override
	public void onNewConnection(User user) {
		
		runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				
			}
		});
		
		Intent intent = new Intent(getApplicationContext(), BattleActivity.class);
		intent.putExtra(BattleActivity.BATTLE_MODE, BattleActivity.RUNNING_MODE);
		startActivity(intent);
	}

	@Override
	protected void onPause() {
		whileGettingConnected = false;
		super.onPause();
	}

	@Override
	protected void onResume() {
		whileGettingConnected = true;
		super.onResume();
	}

}
